import rclpy
from rclpy.node import Node
from rclpy.action import ActionServer
from base_interfaces.action import Progress
import time
class ProgeressActionServer(Node):
    def __init__(self):
        super(). __init__("progress_action_server_node_py")
        self.get_logger().info("服务端节点已创建")
        self.server=ActionServer(self,Progress,"get_sum",self.execute_callback)

    def execute_callback(self,goal_handle):
         num=goal_handle.request.num
         sum=0
         for i in range(1,num+1):
             sum+=i
             feedback=Progress.Feedback()  
             feedback.progress=i/num
             goal_handle.publish_feedback(feedback)
             self.get_logger().info("连续反馈：%.2f" % feedback.progress)
             time.sleep(1.0)
         goal_handle.succeed()
         result=Progress.Result()
         result.sum=sum
         self.get_logger().info("计算结果：%d" % result.sum)
         return result

def main():
    rclpy.init()
    rclpy.spin(ProgeressActionServer())
    rclpy.shutdown()    
if __name__=='__main__':
    main()
